package com.leetcode.LC_MED;

public class LC74 {
    public boolean searchMatrix(int[][] matrix, int target) {

        for (int[] ints : matrix) {
            if (target >= ints[0] && target <= ints[matrix[0].length - 1]) {
                return toBin(ints, target);
            }
        }
        return false;
    }

    private boolean toBin(int[] ints, int target) {
        int a = 0, b = ints.length - 1;
        while (a <= b) {
            int mid = a + (b - a) / 2;
            if (ints[mid] == target) {
                return true;
            } else if (ints[mid] > target) {
                b = mid - 1;
            } else if (ints[mid] < target) {
                a = mid + 1;
            }
        }
        return false;
    }
}
